package com.zhangtao.demo.controller;

import com.zhangtao.demo.common.util.LotteryTurntableDataUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/game")
public class GameController {

    @GetMapping(value = "/start")
    public List<String> play(@RequestParam(value = "level") String level) {
        return LotteryTurntableDataUtil.getUserCodeList(level);
    }

    @GetMapping(value = "/reset")
    public void reset() {
        LotteryTurntableDataUtil.resetGame();
    }


    @GetMapping(value = "/getResult")
    public Map<String, Object> getResult() {
        Map<String, Object> info = new HashMap<>();
        info.put("user", LotteryTurntableDataUtil.USERCODE_LEVEL);
        info.put("userName", LotteryTurntableDataUtil.BASIC_USERCODE_USERNAME);
        info.put("count", LotteryTurntableDataUtil.LEVEL_USERCOUNT);
        Map<String, List<String>> leveUserMap = new HashMap<>(LotteryTurntableDataUtil.LEVEL_NAME.size());
        LotteryTurntableDataUtil.USERCODE_LEVEL.forEach((v, k) -> {
            List<String> old = leveUserMap.get(k);
            if (old == null) {
                old = new ArrayList<>();
            }
            old.add(v);
            leveUserMap.put(k, old);
        });
        info.put("levelInfo", leveUserMap);
        return info;
    }
    @GetMapping(value = "/getLevelList")
    public Map<String, String> getLevelList() {
        return LotteryTurntableDataUtil.LEVEL_NAME;
    }

    @GetMapping(value = "/setCurrentLevel")
    public boolean setCurrentLevel(@RequestParam("level") String level) {
        LotteryTurntableDataUtil.setCurrentLevel(level);
        return true;
    }
    @GetMapping(value = "/setLevel")
    public boolean setLevel(@RequestParam("level") String level, @RequestParam("name") String name,
                            @RequestParam("count") int count, @RequestParam("optCount") int optCount) {
        LotteryTurntableDataUtil.setLevel(level, name, count, optCount);
        return true;
    }

    @GetMapping(value = "/getSetInfo")
    public Map<String, Object> getSetInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("level_name", LotteryTurntableDataUtil.LEVEL_NAME);
        info.put("level_count", LotteryTurntableDataUtil.LEVEL_COUNT);
        info.put("level_opt_count", LotteryTurntableDataUtil.PER_OPT_COUNT);
        info.put("current_level", LotteryTurntableDataUtil.currentLevelCode);
        return info;
    }
}
